//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：HDMEditor
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace HDMCode.FrameEditor
{
    public class HDMEditor : EditorWindow
    {
        /// <summary>
        /// 当前编辑器窗口
        /// </summary>
        public static HDMEditor thisWindow { get; set; }

        /// <summary>
        /// 当前菜单绘制位置
        /// </summary>
        private Vector2 menusScrollPosition;

        /// <summary>
        /// 用于保存绘制方法的字典
        /// </summary>
        public Dictionary<string, List<(DrawMenu drawInfo, MethodInfo drawFuntion)>> drawDicr { get; set; }

        /// <summary>
        /// 用于绘制主页的方法
        /// </summary>
        (DrawMenu info, MethodInfo fun) homePage;

        /// <summary>
        /// 当前绘制页面
        /// </summary>
        (DrawMenu info, MethodInfo fun) curPage;

        /// <summary>
        /// Editor初始化生命周期
        /// </summary>
        public void OnEnable()
        {
            ///指定窗口
            HDMEditorDate.thisWindow = thisWindow ??= this;
            HDMEditorDate.packedSetting = AssetDatabase.LoadAssetAtPath<HDMEditorSetting>(HDMEditorDate.PackedAssetsPath);
            GetPackedSetting();
        }

        /// <summary>
        /// Editor绘制生命周期
        /// </summary>
        public void OnGUI()
        {
            HDMEditorStyle.InitStyle();
            DrawDrawList();
            if (GUI.changed)
                thisWindow.SaveChanges();
        }

        /// <summary>
        /// 读取包体配置文件
        /// </summary>
        public void GetPackedSetting() 
        {
            foreach (var packaed in HDMEditorDate.packedSetting.downloadPackedInfo)
            {
                if (HDMEditorPackManager.CheckPackage(packaed.packageName))
                {
                    packaed.isInstall = true;
                    var types = packaed.GetEditorAssembly().GetTypes();
                    foreach (var type in types)
                    {
                        var messats = type.GetMethods();
                        foreach (var messat in messats)
                        {
                            HDMEditorMenuAttribute attribute = (HDMEditorMenuAttribute)messat.GetCustomAttribute(typeof(HDMEditorMenuAttribute));
                            if (attribute != null)
                            {
                                if (messat.IsStatic)
                                {
                                    ///设置按钮事件
                                    AddDrawList(attribute.draw, messat);
                                    ///设置初始页面
                                    if (string.Equals(packaed.packageName, "com.hdmframe.core"))
                                        curPage = homePage = (attribute.draw, messat);
                                }
                                else
                                    Debug.LogError($"请确保使用【HDMEditorMenuAttribute】特性的方法是静态的: {type} -- {messat.Name}");
                            }
                        }
                    }
                }
                else
                    packaed.isInstall = false;
            }
        }

        /// <summary>
        /// 添加到绘制列表
        /// </summary>
        /// <param name="drawMenu"> 需要绘制的菜单 </param>
        /// <param name="drawFuntion"> 需要执行的绘制方法 </param>
        public void AddDrawList(DrawMenu drawMenu, MethodInfo drawFuntion)
        {
            drawDicr ??= new Dictionary<string, List<(DrawMenu drawInfo, MethodInfo drawFuntion)>>();

            if (drawDicr.ContainsKey(drawMenu.classify))
            {
                List<(DrawMenu drawInfo, MethodInfo drawFuntion)> drawValue = default;
                if (drawDicr.TryGetValue(drawMenu.classify, out drawValue))
                    drawValue.Add((drawMenu, drawFuntion));
                drawDicr[drawMenu.classify] = drawValue;
            }
            else
            {
                List<(DrawMenu drawInfo, MethodInfo drawFuntion)> drawValue = new List<(DrawMenu drawInfo, MethodInfo drawFuntion)>();
                drawValue.Add((drawMenu, drawFuntion));
                drawDicr.Add(drawMenu.classify, drawValue);
            }
        }

        public void DrawDrawList() 
        {
            GUILayout.BeginHorizontal();
            {
                Rect menu = new Rect(0, 0, 140, 600);
                Rect main = new Rect(140, 0, 450, 600);

                GUILayout.BeginArea(menu);
                {
                    HDMEditorHelper.DrawXLine();
                    HDMEditorHelper.DrawButton($"","BackHome.png", () =>{ curPage = homePage ;});
                    HDMEditorHelper.DrawXLine();
                    menusScrollPosition = GUILayout.BeginScrollView(menusScrollPosition, false, false, HDMEditorStyle.styleScrollbarA, HDMEditorStyle.styleScrollbarB);
                    DrawEditorMenu();
                    GUILayout.EndScrollView();
                    GUILayout.Space(15);
                    GUILayout.EndArea();
                }

                GUILayout.BeginArea(main);
                {
                    using (new GUILayout.HorizontalScope())
                    {
                        HDMEditorHelper.DrawYLine();
                        GUILayout.BeginScrollView(new Vector2(0, 0), GUILayout.MinWidth(450), GUILayout.MaxWidth(450));
                        {
                            switch (curPage.info.drawType)
                            {
                                case HDMEditorDrawType.Draw:
                                    curPage.fun?.Invoke(default, default);
                                    break;
                                case HDMEditorDrawType.Once:
                                    curPage.fun?.Invoke(default, default);
                                    curPage.fun = null;
                                    break;
                                case HDMEditorDrawType.None:
                                    break;
                            }
                            GUILayout.EndScrollView();
                        }
                    }
                    GUILayout.EndArea();
                }
            }
            GUILayout.EndHorizontal();
        }

        /// <summary>
        /// 绘制菜单
        /// </summary>
        public void DrawEditorMenu() 
        {
            foreach (var item in drawDicr)
            {
                if (string.IsNullOrEmpty(item.Key))
                    continue;

                HDMEditorHelper.DrawTileLable(item.Key);
                foreach (var value in item.Value)
                {
                    if (value.drawInfo.menuName=="主页")
                        continue;
                    HDMEditorHelper.DrawButton($"    {value.drawInfo.menuName}", value.drawInfo.iconName, () => 
                    {
                        curPage = value;
                    });
                }
            }
        }
    }
}


